package com.baosight.risk.service.jlta.impl;

import com.baosight.risk.api.jlta.JltaNoticeService;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.utils.ShiroUtils;
import com.baosight.risk.mapper.jlta.JltaNoticeMapper;
import com.baosight.risk.mapper.jlta.JltaTeamUserMapper;
import com.baosight.risk.service.jlta.entity.JltaNotice;
import com.baosight.risk.service.jlta.entity.JltaTeamUser;
import com.baosight.risk.service.jlta.vo.JltaNoticeVO;
import com.baosight.risk.service.jlta.vo.MutiTeamVO;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 作者： lishaoqing
 * 时间： 2019年07月09日
 * 描述： JltaNoticeServiceImpl
 * 版本: v1.0
 */
@Service
public class JltaNoticeServiceImpl implements JltaNoticeService {

    @Autowired
    private JltaNoticeMapper jltaNoticeMapper;

    @Autowired
    private JltaTeamUserMapper teamUserMapper;

    @Override
    public List<JltaNotice> selectNoticeList(JltaNotice jltaNotice) {
        XtzyUser user = ShiroUtils.getUser();
        jltaNotice.setCreateBy(user.getUserCode());
        return jltaNoticeMapper.selectNoticeList(jltaNotice);
    }

    @Override
    public Integer addNotice(MutiTeamVO mutiTeamVO) {
        //添加公告
        XtzyUser user = ShiroUtils.getUser();
        String userCode = user.getUserCode();
        JltaNoticeVO vo = mutiTeamVO.getJltaNoticeVO();
        String taskId = UUID.randomUUID().toString();
        Date now = new Date();
        //判断权限
        JltaTeamUser userQuery = new JltaTeamUser();
        userQuery.setUserCode(userCode);
        userQuery.setTeamId(mutiTeamVO.getTeamId());
        JltaTeamUser jltaTeamUser = teamUserMapper.selectOne(userQuery);
        if ("0".equals(jltaTeamUser.getRole())) {
            return 0;
        }
        if (vo != null) {
            JltaNotice jltaNotice = new JltaNotice();
            jltaNotice.setId(taskId);
            jltaNotice.setNoticeName(vo.getNoticeName());
            jltaNotice.setNoticeBody(vo.getNoticeBody());
            jltaNotice.setTeamId(vo.getTeamId());
            jltaNotice.setTeamName(vo.getTeamName());
            jltaNotice.setCreateBy(userCode);
            jltaNotice.setCreateTime(now);
            jltaNotice.setStatus("0");
            jltaNotice.setStatusName("草稿");
            jltaNoticeMapper.insert(jltaNotice);
        }
        return 1;
    }

    @Override
    public Integer editNotice(MutiTeamVO mutiTeamVO) {
        //编辑公告内容
        XtzyUser user = ShiroUtils.getUser();
        String userCode = user.getUserCode();
        JltaNoticeVO vo = mutiTeamVO.getJltaNoticeVO();
        JltaNotice jltaNotice1 = new JltaNotice();
        jltaNotice1.setId(vo.getId());
        JltaNotice jltaNotice = jltaNoticeMapper.selectOne(jltaNotice1);
        String createBy = jltaNotice.getCreateBy();
        //封装查询条件
        JltaTeamUser userQuery = new JltaTeamUser();
        userQuery.setUserCode(userCode);
        userQuery.setTeamId(mutiTeamVO.getTeamId());
        JltaTeamUser jltaTeamUser = teamUserMapper.selectOne(userQuery);
        if ((!createBy.equals(userCode) && "0".equals(jltaTeamUser.getRole()))) {
            return 0;
        }
        jltaNotice.setNoticeName(vo.getNoticeName());
        jltaNotice.setNoticeBody(vo.getNoticeBody());
        jltaNotice.setUpdateBy(userCode);
        jltaNotice.setUpdateTime(new Date());
        jltaNoticeMapper.editNotice(jltaNotice);
        return 1;
    }

    @Override
    public Integer deleteNotice(MutiTeamVO mutiTeamVO) {
        //更新状态为已删除
        XtzyUser user = ShiroUtils.getUser();
        String userCode = user.getUserCode();
        JltaNoticeVO vo = mutiTeamVO.getJltaNoticeVO();
        JltaNotice jltaNotice1 = new JltaNotice();
        jltaNotice1.setId(vo.getId());
        JltaNotice jltaNotice = jltaNoticeMapper.selectOne(jltaNotice1);
        String createBy = jltaNotice.getCreateBy();
        //封装查询条件
        JltaTeamUser userQuery = new JltaTeamUser();
        userQuery.setUserCode(user.getUserCode());
        userQuery.setTeamId(mutiTeamVO.getTeamId());
        JltaTeamUser jltaTeamUser = teamUserMapper.selectOne(userQuery);
        if ((!createBy.equals(userCode) && "0".equals(jltaTeamUser.getRole()))) {
            return 0;
        }
        jltaNotice.setStatus("2");
        jltaNotice.setStatusName("禁用");
        jltaNotice.setUpdateBy(userCode);
        jltaNotice.setUpdateTime(new Date());
        jltaNoticeMapper.deleteNotice(jltaNotice);
        return 1;
    }


    @Override
    public Integer releaseNotice(List<String> ids) {
        //更新状态为已发布
        XtzyUser user = ShiroUtils.getUser();
        String userCode = user.getUserCode();
        for (String id : ids) {
            JltaNotice jltaNotice = new JltaNotice();
            jltaNotice.setId(id);
            jltaNotice.setStatus("1");
            jltaNotice.setStatusName("已发布");
            jltaNotice.setUpdateBy(userCode);
            jltaNotice.setUpdateTime(new Date());
            jltaNotice.setPublishId(user.getUserCode());
            jltaNotice.setPublishName(user.getUserDesc());
            jltaNoticeMapper.deleteNotice(jltaNotice);
        }
        return 0;
    }

}
